package com.isesol.ime.validator.message;

import com.isesol.arch.fluentvalidator.*;
import com.isesol.ime.dal.mapper.*;
import com.isesol.ime.dal.model.*;
import com.isesol.ime.dal.model.dto.*;
import com.isesol.ime.enums.*;
import org.apache.commons.collections.*;
import org.apache.commons.lang3.*;
import org.springframework.stereotype.*;

import javax.annotation.*;

@Component
public class MessageCreateValidator extends ValidatorHandler<MessageDTO> implements Validator<MessageDTO> {

    @Resource
    MessageMapper messageMapper;

    @Resource
    ParamMapper paramMapper;

    @Override
    public boolean validate(ValidatorContext context, MessageDTO messageDTO) {

        boolean result = true;



        if (StringUtils.isEmpty(messageDTO.getParams())) {

            context.addErrorMsg("params error,paramIds is null");
            result = false;
        }

        if (StringUtils.isEmpty(messageDTO.getMsgId())) {

            context.addErrorMsg("params error,msgId is null");
            result = false;

        }
        if (StringUtils.isEmpty(messageDTO.getMsgName())) {

            context.addErrorMsg("params error,msgName is null");
            result = false;

        }
        if (StringUtils.isEmpty(messageDTO.getMsgNumber())) {

            context.addErrorMsg("params error,msgNumber is null");
            result = false;

        }
        if (StringUtils.isEmpty(messageDTO.getRuleType())) {

            context.addErrorMsg("params error,ruleType is null");
            result = false;

        }
        if (StringUtils.isEmpty(messageDTO.getRule())) {

            context.addErrorMsg("params error,rule is null");
            result = false;

        }

        if(StringUtils.isNotEmpty(messageDTO.getMsgId())){

            if (CollectionUtils.isNotEmpty(messageMapper.findByMsgIdAndStatus(messageDTO.getMsgId(), StatusEnum.ON.getStatus()))) {

                context.addErrorMsg("msdId already exist");
                result = false;

            }
        }

        if(StringUtils.isNotEmpty(messageDTO.getParams())){

            String paramIds = messageDTO.getParams();

            String[] paramIdArray = paramIds.split(",");

            for (int i = 0; i < paramIdArray.length; i++) {

                ParamDO paramDO = paramMapper.getById(Long.parseLong(paramIdArray[i]));

                if (null == paramDO) {

                    context.addErrorMsg("param not exist , id = " + paramIdArray[i]);
                    result = false;
                }
            }

            if (paramsAllExistVerify(paramIds)){

                if (!paramsVersionVerify(paramIds)) {

                    context.addErrorMsg("paramVersion not the same");
                    result = false;

                }
            }
        }

        return result;
    }

    public boolean paramsVersionVerify(String paramIds) {

        String[] paramIdArray = paramIds.split(",");

        String paramVersion = paramMapper.getById(Long.parseLong(paramIdArray[0])).getParamVersion();

        for (int i = 1; i < paramIdArray.length; i++) {

            ParamDO paramDO = paramMapper.getById(Long.parseLong(paramIdArray[i]));

            if (!paramVersion.equals(paramDO.getParamVersion())) {

                return false;

            }
        }

        return true;
    }

    public boolean paramsAllExistVerify(String paramIds) {

        String[] paramIdArray = paramIds.split(",");

        for (int i = 0; i < paramIdArray.length; i++) {

            ParamDO paramDO = paramMapper.getById(Long.parseLong(paramIdArray[i]));

            if (null == paramDO) {

                return false;
            }
        }
        return true;
    }

}